package org.tissou.server.importer.xls;

import org.tissou.server.Operations.*;
import org.tissou.server.Utils.Variables_Globales;
import org.tissou.server.Utils.InterfaceMySQL;
import org.tissou.server.Base.Proprietaire;
import org.tissou.server.Base.Categorie;
import org.tissou.server.Services.CategorieServiceImpl;

import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;
import java.text.ParseException;

import org.tissou.client.Services.Compte;
import org.tissou.client.Services.CategorieInfos;
import org.tissou.client.Services.Dette;
import org.tissou.client.Services.CategorieService;
import org.tissou.client.Panel_Premier.operations_tab.Operations.Operation_VirementInterne;

/**
 * Created by IntelliJ IDEA.
 * User: Manu
 * Date: 27 mars 2009
 * Time: 08:08:22
 * To change this template use File | Settings | File Templates.
 */
public class ExcelImporter {

  public static SimpleDateFormat SDF = new SimpleDateFormat("dd/MM/yyyy");

  public Categorie Tout = Categorie.chargeage(0, new InterfaceMySQL());

  public void importer() throws IOException, ParseException {
    // BufferedReader f = new BufferedReader(new FileReader("P:\\Docs\\depense.csv"));
    // BufferedReader f = new BufferedReader(new FileReader("P:\\Docs\\recetteclaire.csv"));
    BufferedReader f = new BufferedReader(new FileReader("P:\\Docs\\pret.csv"));
    String line = null;

    int nbLignes = 0;
    List<String> all;
    int row = 0;
    int col = 0;

    // read each line of text file
    while((line = f.readLine()) != null) {
      StringTokenizer st = new StringTokenizer(line,";");
      all = new ArrayList<String>();
      while (st.hasMoreTokens()) {
        //get next token and store it in the array
        all.add(st.nextToken());


      }
      while (all.size() < 9) {
        all.add("");
      }

      // System.out.println(String.format("Ligne %d", nbLignes++));
      insererLignePret(all);
      row++;
    }

    //close the file
    f.close();
  }

  // depense
   private void insererLigneVirementInterne(List<String> all) throws ParseException {

     Date d = SDF.parse(all.get(0));
     Compte comptedeQui = null;

     Proprietaire dequi = null;

     int noCompte = Integer.valueOf(all.get(1));
     for (Proprietaire p : Variables_Globales.ListeProprietaire.getListe()) {
       for (Compte compte : p.ObtenirTousLesComptes()) {
         if (compte.getNoCompte() == noCompte) {
           comptedeQui = compte;
           dequi = p;
         }
       }
     }

     Proprietaire aqui = null;
     Compte compteAQui = null;

     int noCompteAqui = Integer.valueOf(all.get(2));
     for (Proprietaire p : Variables_Globales.ListeProprietaire.getListe()) {
       for (Compte compte : p.ObtenirTousLesComptes()) {
         if (compte.getNoCompte() == noCompteAqui) {
           compteAQui = compte;
           aqui = p;
         }
       }
     }

     float somme;
     somme = Float.valueOf(all.get(3).replace(',', '.'));


     Operation_VirInterne op = new Operation_VirInterne(d, comptedeQui, compteAQui, somme, true);
    op.Entrer_Transactions();
   }


  // depense
  private void insererLigneRemboursement(List<String> all) throws ParseException {

    Date d = SDF.parse(all.get(0));
    Compte comptedeQui = null;

    Proprietaire dequi = null;

    int noCompte = Integer.valueOf(all.get(1));
    for (Proprietaire p : Variables_Globales.ListeProprietaire.getListe()) {
      for (Compte compte : p.ObtenirTousLesComptes()) {
        if (compte.getNoCompte() == noCompte) {
          comptedeQui = compte;
          dequi = p;
        }
      }
    }

    Proprietaire aqui = null;
    Compte compteAQui = null;

    int noCompteAqui = Integer.valueOf(all.get(2));
    for (Proprietaire p : Variables_Globales.ListeProprietaire.getListe()) {
      for (Compte compte : p.ObtenirTousLesComptes()) {
        if (compte.getNoCompte() == noCompteAqui) {
          compteAQui = compte;
          aqui = p;
        }
      }
    }

    float somme;
    somme = Float.valueOf(all.get(3).replace(',', '.'));


    Operation_Remboursement op = new Operation_Remboursement(d, comptedeQui, compteAQui, somme, true);
    op.Entrer_Transactions();
  }

  // depense
  private void insererLignePret(List<String> all) throws ParseException {

    Date d = SDF.parse(all.get(0));
    Compte comptedeQui = null;

    Proprietaire dequi = null;

    int noCompte = Integer.valueOf(all.get(1));
    for (Proprietaire p : Variables_Globales.ListeProprietaire.getListe()) {
      for (Compte compte : p.ObtenirTousLesComptes()) {
        if (compte.getNoCompte() == noCompte) {
          comptedeQui = compte;
          dequi = p;
        }
      }
    }

    Proprietaire aqui = null;
    Compte compteAQui = null;

    int noCompteAqui = Integer.valueOf(all.get(2));
    for (Proprietaire p : Variables_Globales.ListeProprietaire.getListe()) {
      for (Compte compte : p.ObtenirTousLesComptes()) {
        if (compte.getNoCompte() == noCompteAqui) {
          compteAQui = compte;
          aqui = p;
        }
      }
    }

    float somme;
    somme = Float.valueOf(all.get(3).replace(',', '.'));


    Operation_Pret op = new Operation_Pret(d, comptedeQui, compteAQui, somme, true);
    op.Entrer_Transactions();
  }

  // depense
  private void insererLigneRecette(List<String> all) throws ParseException {

    Date d = SDF.parse(all.get(0));
    Compte c = null;

    Proprietaire dequi = null;

    int noCompte = Integer.valueOf(all.get(1));
    for (Proprietaire p : Variables_Globales.ListeProprietaire.getListe()) {
      for (Compte compte : p.ObtenirTousLesComptes()) {
        if (compte.getNoCompte() == noCompte) {
          c = compte;
          dequi = p;
        }
      }
    }

    float somme;
    somme = Float.valueOf(all.get(4).replace(',', '.'));

    String categorie = all.get(2);
    if (categorie.equalsIgnoreCase("Virement interne") || categorie.equalsIgnoreCase("pret")) {
      System.out.println("C'est un virement interne -> Poubelle.");
      return;
    }
    String l = all.get(3);
    CategorieInfos cat = getCategorie(categorie, l);

    List<Dette> dettes = new ArrayList<Dette>();

    Operation_Recette op = new Operation_Recette(d, c, cat, l, somme, true, dettes);
    op.Entrer_Transactions();
  }
  // depense
  private void insererLigne(List<String> all) throws ParseException {

    Date d = SDF.parse(all.get(0));
    Compte c = null;

    Proprietaire dequi = null;

    int noCompte = Integer.valueOf(all.get(1));
    for (Proprietaire p : Variables_Globales.ListeProprietaire.getListe()) {
      for (Compte compte : p.ObtenirTousLesComptes()) {
        if (compte.getNoCompte() == noCompte) {
          c = compte;
          dequi = p;
        }
      }
    }

    float somme;
    somme = Float.valueOf(all.get(4).replace(',', '.'));

    String categorie = all.get(2);
    if (categorie.equalsIgnoreCase("Virement interne") || categorie.equalsIgnoreCase("pret")) {
      System.out.println("C'est un virement interne -> Poubelle.");
      return;
    }
    String l = all.get(3);
    CategorieInfos cat = getCategorie(categorie, l);

    List<Dette> dettes = new ArrayList<Dette>();
    String nomAQui = all.get(7);

    if (!nomAQui.equals("")) {
      float cbAQui;
      try {
      cbAQui = Float.valueOf(all.get(8).replace(',', '.'));
      } catch (NumberFormatException e) {
        System.out.println(":(");
        cbAQui = 0;
      }
      Proprietaire aqui =Variables_Globales.ListeProprietaire.ObtenirProprietaire(nomAQui);
      dettes.add(new Dette(dequi.getInfos(), aqui.getInfos(), cbAQui));
    }

    Operation_Depense op = new Operation_Depense(d, c, cat, l, somme, true, dettes);
    op.Entrer_Transactions();
  }

  private CategorieInfos getCategorie(String categorie, String libelle) {
    CategorieInfos cat =  null;
    categorie = categorie.toLowerCase();
    libelle = libelle.toLowerCase();
    Map<String, String> mapping = new HashMap<String, String>();
    mapping.put("appart - loyer", "Appart - Loyer");
    mapping.put("m&s", "M&S");
    mapping.put("sainsbury", "Sainsbury's");
    mapping.put("sainsburys", "Sainsbury's");
    mapping.put("sainsbury's", "Sainsbury's");
    mapping.put("saintsbury's", "Sainsbury's");
    mapping.put("sainsbury's", "Sainsbury's");
    mapping.put("appart - charges", "Appart - Charges");
    mapping.put("transfert bnp -> pound", "Transfert - euro vers pound");
    mapping.put("tendon casse", "Sante");
    mapping.put("r,tablissement", "Retablissement");
    mapping.put("sant,", "Sante");
    mapping.put("sant,", "Sante");
    if (mapping.containsKey(categorie)) {
      cat = Tout.rechercherCategorieDansDescendance(mapping.get(categorie)).getInfos();
    } else if (mapping.containsKey(libelle)) {
      cat = Tout.rechercherCategorieDansDescendance(mapping.get(libelle)).getInfos();
    } else {
      cat = Tout.rechercherCategorieDansDescendance(categorie).getInfos();
      if (!cat.getNom().equalsIgnoreCase(categorie)) {
      System.out.println("            -> " + categorie + " -> " + cat.getNom());
      }
    }
    return cat;
  }

}

